package com.dao.impl;

import com.Bean.Client;
import com.Bean.OtherGoods;
import com.Bean.PaymentTerm;
import com.Bean.RoomList;
import com.dao.CheckInDao;
import com.util.JdbcUtil;
import com.util.JsonUtil;
import com.util.RowMapper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//开房
public class CheckInDaoImpl implements CheckInDao {
    @Override
    public int insert(Object o) throws SQLException {
        return 0;
    }

    @Override
    public List queryData(Object o) throws IllegalAccessException, SQLException, InstantiationException {
        return null;
    }

    @Override
    public int update(Object o) throws SQLException {
        return 0;
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        return 0;
    }
    //根据身份证查询是否有该会员信息
    public Client SelectIdCard(String idcard) throws SQLException {
        String sql="select * from client where pid=?";
        Client client = JdbcUtil.SelectIdCard(sql, idcard);
        return client;

    }

    //更新房间为状态
    public void changeroomstate(String rid,String rstatus,String timetype) throws SQLException {

        String sql="update room_id set rstatus=?,timetype=? WHERE rid=? ";
        int i = JdbcUtil.insertOrUpdateOrDelete(sql, new Object[]{rstatus,timetype, rid});
    }
    //插入订单
    public void insertdd(RoomList roomList,String sid) throws SQLException {

        String sql="insert into room_list(lid,rid,pno,sid,eid,ltype,intime,outtime,ldate,lsum,lcash,lillegal,lin_status,out_status,lroomprice,lotherprice,deposit) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        int i = JdbcUtil.insertOrUpdateOrDelete(sql, new Object[]{roomList.getLid(),roomList.getRid(),roomList.getPno(),roomList.getSid(),sid,roomList.getLtype(),roomList.getIntime(),
                roomList.getOuttime(),roomList.getLdate(),roomList.getLsum(),roomList.getLcash(),roomList.getLillegal(),roomList.getLinStatus(),roomList.getOutStatus(),
                roomList.getLroomprice(),roomList.getLotherprice(),roomList.getDeposit()});
    }

    public void Rent(String rid, String rstatus, String username, RoomList roomList, List<String> goodsList, Client client, Boolean flag ,String timetype) throws SQLException {
        //1.改房间状态
        changeroomstate(rid,rstatus,timetype);
        //2.新增或更新顾客信息
        if(flag){
            //更新会员
            String sql="UPDATE client SET ptotal=?, pcount=?,lasttime=? where pid=?";
            JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{client.getPtotal(),client.getPcount(),client.getLasttime(),client.getPid()});
        }else{
            //新增会员
            String sql="insert into client(pno,cli_pid,pname,pid,pphone,ptotal,pcount,lasttime,pcretime) values(?,?,?,?,?,?,?,?,?)";
            JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{client.getPno(),client.getCliPid(),client.getPname(),client.getPid(),
                    client.getPphone(),client.getPtotal(),client.getPcount(),client.getLasttime(),client.getPcretime()});
        }
        roomList.setPno(client.getPno());
        //3.根据操作员姓名查询用户名id
        if (username==""||username==null){
            username="李建辉";
        }
        String sql="select eid from employee where ename=?";
        String sid = JdbcUtil.queryuId(sql, username);

        //4.插入订单信息
        insertdd(roomList,sid);
        //5.插入商品消费表
        for (int j = 0; j < goodsList.size(); j++) {
            String otherGoods1 = goodsList.get(j);
            System.out.println("otherGoods1:"+otherGoods1);
            String sql2="select gid from goods where gname=?";
            String gid = JdbcUtil.queryGoodsId(sql2,otherGoods1);//得到其他消费编号

            String sql1="insert into cosume(cid,lid,gid) values(?,?,?)";
            int i = JdbcUtil.insertOrUpdateOrDelete(sql1, new Object[]{(System.currentTimeMillis() + "").substring(5), roomList.getLid(), gid});

        }

    }
    public List<PaymentTerm> SelectPayType() throws SQLException {
        String sql="select * from payment_term";
        List<PaymentTerm> querypaytype = JdbcUtil.querypaytype(sql);
        return  querypaytype;
    }
    //根据房间id查找最近的一个订单id
    public String SelectLidWererid(String rid) throws SQLException {
        String sql="select lid from room_list,(select MAX(ldate) maxdate from room_list where rid=?) a where a.maxdate=room_list.ldate  and room_list.rid=?";
        String s = JdbcUtil.querylId(sql, rid);
        return s;
    }
    //根据订单id查询订单价格
    public RoomList SelectLidWererTotalPrice(String lid) throws SQLException {
        String sql="select lroomprice,lotherprice,lcash,lsum from room_list where lid =?";
        RoomList roomList = JdbcUtil.queryTotalPricewherelId(sql, lid);
        return roomList;
    }
    //更新 预定订单为入住
    public void updatedd(String lid,String rid,String paytype) throws SQLException {

        String sql1="update room_list set out_status='已收齐',lin_status='已入住',sid=? where lid=?";//订单状态
        String sql2="update room_id set rstatus='已住' where rid=?";//房间状态

        int i1 = JdbcUtil.insertOrUpdateOrDelete(sql1, new Object[]{paytype,lid});
        int i2 = JdbcUtil.insertOrUpdateOrDelete(sql2, new Object[]{rid});
    }
    //更新 已住订单为已退房状态
    public void updateddtozan(String lid,String rid) throws SQLException {
        System.out.println("lid:"+lid+",rid:"+rid);
        String sql1="update room_list set lin_status='已退',deposit=2 where lid=?";//订单状态
        String sql2="update room_id set rstatus='脏房' where rid=?";//房间状态

        int i1 = JdbcUtil.insertOrUpdateOrDelete(sql1, new Object[]{lid});
        int i2 = JdbcUtil.insertOrUpdateOrDelete(sql2, new Object[]{rid});
    }
    public Double Selectlcash(String lid) throws SQLException {
        String sql="select lcash from room_list where lid=?";
        Double queryllcash = JdbcUtil.queryllcash(sql, lid);
        return queryllcash;
    }
    public void updateddtokon(String rid) throws SQLException{
        String sql="update room_id set rstatus='空房' where rid=?";
        JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{rid});
    }

}
